/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pruebita01;
import javax.swing.*;
import javax.swing.JTable;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.lang.Object;
import java.util.Random;
import java.util.Arrays;

public class WPanel extends JPanel
{
	private JPanel p1, p2, p3, p4, p5;
	private JButton b1, b3;
	private JLabel l1,l2,l3,l4;
	private JComboBox list1, list2, list3, list4;
	private int[] processId;
	private int[] processSize;
	private int[] partitionId;
	private int[] partitionSize;
	private int[] compareArray;
	private int intFrag;
	private int extFrag;
   public WPanel()
   {
   	/*getContentPane().setLayout(new GridLayout(5,1));
		setTitle("Memory Management using Worst-Fit Algorithm");
		setSize(500, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/

		p1 = new JPanel();
   		p1.setLayout(new GridLayout(1,2));

   		String[] processList =  {"2","3","4","5","6","7","8","9","10"};
   		String[] processSize = {"200","400","600","800","1000","2000"};
   		l1 = new JLabel("No. of Max. Processes: ");
   		l2 = new JLabel("Max. Size of Process(KB): ");

   		list1 = new JComboBox(processList);
   		list2 = new JComboBox(processSize);

   		p2 = new JPanel();
		p2.setLayout(new GridLayout(1,2));

   		p1.add(l1);
   		p1.add(list1);
   		p2.add(l2);
   		p2.add(list2);

		String[] partition = {"2", "3", "4", "5"};
		l3 = new JLabel("No. of partitions");

		list3 = new JComboBox(partition);

		p3 = new JPanel();
		p3.setLayout(new GridLayout(1,2));
		p4 = new JPanel();
		p4.setLayout(new GridLayout(1,2));

	   	p3.add(l3);
		p3.add(list3);

		  p5 = new JPanel();
		  p5.setLayout(new GridLayout(1,2));
		  l4 = new JLabel("Max. Size of Partition:");

		  String[] partitionSize = {"500", "1000", "2000", "4000"};
		  list4 = new JComboBox(partitionSize);

		  p5.add(l4);
		  p5.add(list4);

		b1 = new JButton("Execute Worst Fit");
		b3 = new JButton("Main Menu");

		p4.add(b1);
		p4.add(b3);

		add(p1);
		add(p2);
		add(p3);
		add(p5);
		add(p4);

		//Container WPanel = Frame.getContentPane();
		//WPanel.setLayout(new GridLayout(5,1));

		 b1.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			if(b1.getText()=="Execute Worst Fit")
				getRandom();
				worstFit();
				getFrag();

			}
		});
   }
 public void getRandom(){
		String getPro = list1.getSelectedItem().toString();//get process list
		String getMaxSize = list2.getSelectedItem().toString();//get size of processes
		String getPar = list3.getSelectedItem().toString();//get no of partition
		String getParSize = list4.getSelectedItem().toString();//get size of partition
		int getProInt = Integer.parseInt(getPro);
		int getMaxSizeInt = Integer.parseInt(getMaxSize);
		int getParInt = Integer.parseInt(getPar);
		int getParSizeInt = Integer.parseInt(getParSize);

	   	processId = new int[getProInt];
		processSize = new int[getProInt];
		partitionId = new int[getParInt];
		partitionSize = new int[getParInt];


		Random randomGenerator = new Random();

		for(int data = 0; data < getProInt; data++){
			processId[data] = data;
			processSize[data] = randomGenerator.nextInt(getMaxSizeInt);
		}

		partitionId = new int[getParInt];
		partitionSize = new int[getParInt];
		compareArray = new int[getParInt];

		for(int dataTwo = 0; dataTwo < getParInt; dataTwo++){
			partitionId[dataTwo] = dataTwo;
			partitionSize[dataTwo] = randomGenerator.nextInt(getParSizeInt);
			compareArray[dataTwo] = partitionSize[dataTwo];

		}

		orderPartition();

		System.out.println();
		System.out.println("*Processes Generated*:");
		System.out.println("------------------------------------------");
		for(int check = 0; check < processId.length; check++){
			System.out.println("processId: "+ check + "; Value: " + processSize[check]+"KB");
		}
		System.out.println("------------------------------------------");
		System.out.println();
		System.out.println("*Partitions Avaliable*:");
		System.out.println("------------------------------------------");
			for(int check = 0; check < partitionId.length; check++){
			System.out.println("partitionId: "+ check + "; Value: " + partitionSize[check]+"KB");

		}
		System.out.println("------------------------------------------");
		System.out.println();

	}

	public void orderPartition()
	{
		Arrays.sort(partitionSize);
		Arrays.sort(compareArray);

	}

	public void getFrag()
	{
		int result = 0;
		int counter = 0;
		int totalFrag = 0;
		extFrag = 0;
		intFrag = 0;

		while(counter<partitionSize.length)
		{
			Arrays.sort(partitionSize);
			int b = Arrays.binarySearch(compareArray, partitionSize[counter]);
			if( b >=0)
			{
				extFrag = extFrag + partitionSize[counter];
				counter++;

			}
			else
				counter++;

		}
		for(int a = 0; a < partitionSize.length; a++)
		{
			totalFrag = totalFrag + partitionSize[a];
		}
		intFrag = totalFrag - extFrag;
		System.out.println("*Results*:");
		System.out.println("------------------------------------------");
		System.out.println("Internal Fragmentation: " + intFrag + "KB");
		System.out.println("External Fragmentation: " + extFrag+"KB");
		System.out.println("------------------------------------------");
	}


	public void worstFit()
	{
		int counter = 0;
		int biggestPart = partitionSize.length-1;
	   	boolean [] processLoadStatus = new boolean[processSize.length];

		while(counter < (processSize.length))
		{
			if((partitionSize[biggestPart] > processSize[counter]))
			{

				partitionSize[biggestPart] = partitionSize[biggestPart] - processSize[counter];
				processLoadStatus[counter] = true;
				counter++;

			}
			else
			{

				processLoadStatus[counter] = false;
				counter++;



			}
			orderPartition();
		}

		System.out.println();
		System.out.println("*Process Loaded*:");
		System.out.println("------------------------------------------");
		for(int check = 0; check < processLoadStatus.length; check++){
				System.out.println("PId: "+ check + "; Loaded?: " + processLoadStatus[check]);
		}
		System.out.println("------------------------------------------");
	}

}
